package com.example.finalproject

import android.content.Intent
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.activity.enableEdgeToEdge
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.material3.*
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.example.finalproject.experiments.guessgame.GuessGameActivity
import com.example.finalproject.experiments.intent.IntentExperimentActivity
import com.example.finalproject.experiments.lifecycle.LifecycleExperimentActivity
import com.example.finalproject.experiments.sqlite.SQLiteExperimentActivity
import com.example.finalproject.experiments.thread.ThreadExperimentActivity
import com.example.finalproject.experiments.wechat.WeChatExperimentActivity
import com.example.finalproject.experiments.camera.CameraExperimentActivity
import com.example.finalproject.ui.theme.FinalProjectTheme

class MainActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        setContent {
            FinalProjectTheme {
                Surface(
                    modifier = Modifier.fillMaxSize(),
                    color = MaterialTheme.colorScheme.background
                ) {
                    ExperimentList()
                }
            }
        }
    }
}

data class Experiment(
    val title: String,
    val description: String,
    val activity: Class<*>
)

@Composable
fun ExperimentList() {
    val experiments = listOf(
        Experiment(
            "猜拳游戏",
            "简单的石头剪刀布游戏",
            GuessGameActivity::class.java
        ),
        Experiment(
            "Intent实验",
            "Activity间通信及发送短信",
            IntentExperimentActivity::class.java
        ),
        Experiment(
            "Lifecycle实验",
            "Activity生命周期的演示",
            LifecycleExperimentActivity::class.java
        ),
        Experiment(
            "SQLite实验",
            "SQLite数据库的基本操作演示",
            SQLiteExperimentActivity::class.java
        ),
        Experiment(
            "Thread实验",
            "线程与异步任务的演示",
            ThreadExperimentActivity::class.java
        ),
        Experiment(
            "WeChat实验",
            "微信聊天界面及好友列表的演示",
            WeChatExperimentActivity::class.java
        ),
        Experiment(
            "Camera实验",
            "相机预览、拍照及照片列表展示",
            CameraExperimentActivity::class.java
        )
    )

    Column(
        modifier = Modifier
            .fillMaxSize()
            .padding(16.dp)
    ) {
        Text(
            text = "Android实验项目",
            fontSize = 24.sp,
            fontWeight = FontWeight.Bold,
            modifier = Modifier.padding(bottom = 16.dp)
        )

        LazyColumn(
            verticalArrangement = Arrangement.spacedBy(8.dp)
        ) {
            items(experiments) { experiment ->
                ExperimentCard(experiment)
            }
        }
    }
}

@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun ExperimentCard(experiment: Experiment) {
    val context = LocalContext.current

    Card(
        modifier = Modifier
            .fillMaxWidth(),
        onClick = {
            context.startActivity(Intent(context, experiment.activity))
        }
    ) {
        Column(
            modifier = Modifier
                .padding(16.dp)
                .fillMaxWidth()
        ) {
            Text(
                text = experiment.title,
                fontSize = 18.sp,
                fontWeight = FontWeight.Bold
            )
            Spacer(modifier = Modifier.height(4.dp))
            Text(
                text = experiment.description,
                fontSize = 14.sp
            )
        }
    }
}